/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import Pojo.*;
import DAO.Util.*;
import java.io.IOException;
import java.text.ParseException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author An
 */
@WebServlet(name = "Batdauthicontroller", urlPatterns = {"/Batdauthicontroller"})
public class Batdauthicontroller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            String action = request.getParameter("Action");
                if (action != null) {
                    if (action.equals("View")) {
                        String cmnd = request.getParameter("CMND");
                        session.setAttribute("CMND", cmnd);
                        List<Thisinh> ds = ThiSinhDAO.LayThongTinThiSinhTheoCMND_List(cmnd);
                        ArrayList<DapandaCheckPojo> dapandacheck = null;
                        if(ds.size()>0){
                            session.setAttribute("flag", 1);
                            for(int i=0;i<ds.size();i++){
                                Thisinh ts = ds.get(i);
                                session.setAttribute("username_", ts.getHoTen());
                                session.setAttribute("cmnd", ts.getCmnd());
                            }
                            int macapdo = Integer.parseInt(request.getParameter("capdo"));
                            session.setAttribute("capdo",macapdo);
                            Capdo capdo = AdminDAO.LayCapdoTheomacapdo(macapdo);
                            All_Ham_XuLyCauHoi.mangluucauhoidara = new int[capdo.getTongsocau()];
                            DeThiDAO.ThemDeThi(capdo, capdo.getThoigianlambai());
                            List<CauHoi> dsch = All_Ham_XuLyCauHoi.LayDanhSachCauHoiTheoCapDo(capdo);
                            
                            request.setAttribute("dsch", dsch);
                            session.setAttribute("dsch", dsch);
                            session.setAttribute("Capdo", capdo);
                            request.setAttribute("batdauchambai", 0);
                            request.setAttribute("dapandacheck", dapandacheck);
                            request.setAttribute("diemthi", -1);
                            request.setAttribute("socaudung", -1);
                            request.setAttribute("socausai", -1);
                            GregorianCalendar g = new GregorianCalendar(); 
                            session.setAttribute("ngaybatdau",g.getTime());       
                            request.getRequestDispatcher("/Batdauthi.jsp?phutThi=0&giayThi=10").forward(request, response);
                        }
                        else{
                            //System.out.print("CMND NAY KO TON TAI");
                            request.setAttribute("dangnhapthatbai", 0);
                            request.getRequestDispatcher("/DienthongtinFull.jsp").forward(request, response);
                        }
                    } 
                    if(action.equals("batdauthifull")){
                        String cmnd = request.getParameter("CMND");
                        boolean kt = ThiSinhDAO.KiemTraCMNDNayTonTaiChua(cmnd);
                        if(kt==false)
                        {
                            ArrayList<DapandaCheckPojo> dapandacheck = null;
                            Thisinh ts = new Thisinh();
                            String ngay = request.getParameter("Ngay").toString();
                            String thang = request.getParameter("Thang").toString();
                            String nam = request.getParameter("Nam").toString();
                            String date_ = (String)thang +"-"+ ngay +"-"+ nam;
                            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

                            Date ngaysinh = dateFormat.parse(date_);

                            ts.setHoTen(request.getParameter("hoten"));
                            ts.setCmnd(request.getParameter("CMND"));
                            ts.setNgaySinh(ngaysinh);

                            boolean kq = ThiSinhDAO.ThemThiSinh(ts);
                            if(kq==true){
                                session.setAttribute("flag", 1);
                                String hoten = request.getParameter("hoten");
                                session.setAttribute("username_", hoten);
                                session.setAttribute("cmnd", ts.getCmnd());

                                int macapdo = Integer.parseInt(request.getParameter("capdo"));
                                session.setAttribute("capdo",macapdo);
                                Capdo capdo = AdminDAO.LayCapdoTheomacapdo(macapdo);
                                All_Ham_XuLyCauHoi.mangluucauhoidara = new int[capdo.getTongsocau()];
                                DeThiDAO.ThemDeThi(capdo, capdo.getThoigianlambai());
                                List<CauHoi> dsch = All_Ham_XuLyCauHoi.LayDanhSachCauHoiTheoCapDo(capdo);
                                request.setAttribute("dsch", dsch);
                                session.setAttribute("dsch", dsch);
                                session.setAttribute("Capdo", capdo);
                                request.setAttribute("batdauchambai", 0);
                                request.setAttribute("dapandacheck", dapandacheck);
                                request.setAttribute("diemthi", -1);
                                request.setAttribute("socaudung", -1);
                                request.setAttribute("socausai", -1);
                                GregorianCalendar g = new GregorianCalendar(); 
                                session.setAttribute("ngaybatdau",g.getTime()); 
                                request.getRequestDispatcher("/Batdauthi.jsp").forward(request, response);
                            }
                        }else{
                            request.setAttribute("dangnhapthatbai", 2);
                            request.getRequestDispatcher("/DienthongtinFull.jsp").forward(request, response);
                        }
                    }
                    if(action.equals("tieptucthi")){
                            ArrayList<DapandaCheckPojo> dapandacheck = null;
                            
                            int macapdo = Integer.parseInt(request.getParameter("capdo"));
                            session.setAttribute("capdo",macapdo);
                            Capdo capdo = AdminDAO.LayCapdoTheomacapdo(macapdo);
                            All_Ham_XuLyCauHoi.mangluucauhoidara = new int[capdo.getTongsocau()];
                            DeThiDAO.ThemDeThi(capdo, capdo.getThoigianlambai());
                            List<CauHoi> dsch = All_Ham_XuLyCauHoi.LayDanhSachCauHoiTheoCapDo(capdo);
                            
                            request.setAttribute("dsch", dsch);
                            session.setAttribute("dsch", dsch);
                            session.setAttribute("Capdo", capdo);
                            request.setAttribute("batdauchambai", 0);
                            request.setAttribute("dapandacheck", dapandacheck);
                            request.setAttribute("diemthi", -1);
                            request.setAttribute("socaudung", -1);
                            request.setAttribute("socausai", -1);
                            GregorianCalendar g = new GregorianCalendar(); 
                            session.setAttribute("ngaybatdau",g.getTime()); 
                            request.getRequestDispatcher("/Batdauthi.jsp?phutThi=0&giayThi=10").forward(request, response);
                    }
                    if (action.equals("ketquathi")) {
                        int macapdo = Integer.parseInt(session.getAttribute("capdo").toString());
                        Capdo capdo = AdminDAO.LayCapdoTheomacapdo(macapdo);
                        int dapan[] = new int[capdo.getTongsocau()];
                        List<CauHoi> dsch = null;
                        ArrayList<DapandaCheckPojo> dapandacheck = new ArrayList<>();
                        if(session.getAttribute("dsch")!=null)
                        {
                            dsch = (List<CauHoi>)session.getAttribute("dsch"); 
                            for(int i=0;i<dsch.size();i++)
                            {
                                DapandaCheckPojo da = new DapandaCheckPojo();
                                String k = String.valueOf(i+1);
                                if(request.getParameter(k)!=null)
                                {
                                    
                                    int ketquachon = Integer.parseInt(request.getParameter(k));
                                    dapan[i] = ketquachon;
                                    if(ketquachon == 1)
                                    {
                                        da.setA(true);
                                        da.setB(false);
                                        da.setC(false);
                                        da.setD(false);
                                    }
                                    if(ketquachon == 2)
                                    {
                                        da.setA(false);
                                        da.setB(true);
                                        da.setC(false);
                                        da.setD(false);
                                    }
                                    if(ketquachon == 3)
                                    {
                                        da.setA(false);
                                        da.setB(false);
                                        da.setC(true);
                                        da.setD(false);
                                    }
                                    if(ketquachon == 4)
                                    {
                                        da.setA(false);
                                        da.setB(false);
                                        da.setC(false);
                                        da.setD(true);
                                    }
                                }
                                else
                                {
                                    dapan[i] = 0;
                                    da.setA(false);
                                    da.setB(false);
                                    da.setC(false);
                                    da.setD(false);
                                }
                                dapandacheck.add(da);
                            }
                        }
                        float diemthi = All_Ham_XuLyCauHoi.DiemThi(dsch, dapan);
                        int sodaudung = All_Ham_XuLyCauHoi.SoCauDung(dsch, dapan);
                        int socausai = All_Ham_XuLyCauHoi.SoCauSai(dsch, dapan);
                        
                        String cmnd = session.getAttribute("cmnd").toString();
                        int madethi = All_Ham_XuLyCauHoi.madethi;
                        List<Thisinh> dsts = ThiSinhDAO.LayThongTinThiSinhTheoCMND_List(cmnd);
                        Thisinh ts = null;
                        for(int i=0;i<dsts.size();i++){
                            ts = dsts.get(i);
                        }
                        Ketqua ketqua = new Ketqua();
                        ketqua.setDethi(DeThiDAO.LayThongTinDeThiTheoMaDe(madethi));
                        ketqua.setThisinh(ts);
                        
                        ketqua.setThoiGianBatDau((Date)session.getAttribute("ngaybatdau"));
                        GregorianCalendar g = new GregorianCalendar();
                        ketqua.setThoiGianKetThuc(g.getTime());
                        ketqua.setDiem(diemthi);
                        KetquaDAO.ThemKetQua(ketqua);
                        
                        request.setAttribute("batdauchambai", 1);
                        request.setAttribute("dapandacheck", dapandacheck);
                        request.setAttribute("diemthi", diemthi);
                        request.setAttribute("socaudung", sodaudung);
                        request.setAttribute("socausai", socausai);
                        request.getRequestDispatcher("/Batdauthi.jsp?phutThi=0&giayThi=10").forward(request, response);
                    }
                }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(Batdauthicontroller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(Batdauthicontroller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
